package org.jeecg.modules.classroom.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.classroom.entity.FacClassroom;
import org.jeecg.modules.classroom.service.IFacClassroomService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.classroom.vo.FacClassroomVo;
import org.jeecg.modules.common.api.ISysInfo;
import org.jeecg.modules.common.baseentity.BaseApiEntity;
import org.jeecg.modules.relation.relbuildingclassroom.entity.RelBuildingClassroom;
import org.jeecg.modules.relation.relbuildingclassroom.service.IRelBuildingClassroomService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: fac_classroom
 * @author: baochangrong
 * @Date:   2022-02-10
 * @Version: V1.0
 */
@Api(tags="fac_classroom")
@RestController
@RequestMapping("/classroom/facClassroom")
@Slf4j
public class FacClassroomController extends JeecgController<FacClassroom, IFacClassroomService> {
	@Autowired
	private IFacClassroomService facClassroomService;
	@Autowired
	private IRelBuildingClassroomService iRelBuildingClassroomService;
	 @Autowired
	 private ISysInfo sysInfo;
	
	/**
	 * 分页列表查询
	 *
	 * @param facClassroom
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "fac_classroom-分页列表查询")
	@ApiOperation(value="fac_classroom-分页列表查询", notes="fac_classroom-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FacClassroom facClassroom,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		//组装vo以外的其他参数
		Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
		//获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
		BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,facClassroom,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

		facClassroom.setCreateTime(null);
		QueryWrapper<FacClassroom> queryWrapper = QueryGenerator.initQueryWrapper(facClassroom, req.getParameterMap());
		Page<FacClassroom> page = new Page<FacClassroom>(pageNo, pageSize);
		IPage<FacClassroom> pageList = facClassroomService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 获取教室数据列表
	  * listAll
	  */
	 @AutoLog(value = "fac_classroom-列表查询")
	 @ApiOperation(value="fac_classroom-列表查询", notes="fac_classroom-列表查询")
	 @GetMapping(value = "/listAll")
	 public Result<?> listAll(FacClassroom facClassroom,
									HttpServletRequest req) {
		 //组装vo以外的其他参数
		 Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
		 //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
		 BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,facClassroom,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

		 facClassroom.setCreateTime(null);
		 List<FacClassroom> facClassroomList = facClassroomService.list();
		 return Result.OK(facClassroomList);
	 }

	 /**
	  * 附带建筑名称查询 （分页）
	  * voListAll
	  */
	 @AutoLog(value = "fac_classroom-分页列表查询")
	 @ApiOperation(value="fac_classroom-分页列表查询", notes="fac_classroom-分页列表查询")
	 @GetMapping(value = "/voListAll")
	 public Result<?> voListAll(FacClassroomVo facClassroomVo,
							  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
							  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
							  HttpServletRequest req) {

		 //组装vo以外的其他参数
		 Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
		 //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
		 BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,facClassroomVo,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

		 facClassroomVo.setCreateTime(null);
		 Page<FacClassroomVo> page = new Page<FacClassroomVo>(pageNo, pageSize);
		 IPage<FacClassroomVo> pageList = facClassroomService.voListAll(page,facClassroomVo);
		 return Result.OK(pageList);
	 }

	 /**
	 *   添加
	 *
	 * @param facClassroom
	 * @return
	 */
	@AutoLog(value = "fac_classroom-添加")
	@ApiOperation(value="fac_classroom-添加", notes="fac_classroom-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FacClassroom facClassroom,HttpServletRequest req) {
		String tenantId = req.getHeader("tenant-id");
		facClassroom.setTenantId(tenantId);
		facClassroomService.save(facClassroom);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param facClassroom
	 * @return
	 */
	@AutoLog(value = "fac_classroom-编辑")
	@ApiOperation(value="fac_classroom-编辑", notes="fac_classroom-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FacClassroom facClassroom,HttpServletRequest req) {
		String tenantId = req.getHeader("tenant-id");
		facClassroom.setTenantId(tenantId);
		facClassroomService.updateById(facClassroom);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "fac_classroom-通过id删除")
	@ApiOperation(value="fac_classroom-通过id删除", notes="fac_classroom-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		String classroomId = id;
		//删除建筑-教室关系记录
		iRelBuildingClassroomService.removeByClassroomId(classroomId);
		facClassroomService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "fac_classroom-批量删除")
	@ApiOperation(value="fac_classroom-批量删除", notes="fac_classroom-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.facClassroomService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "fac_classroom-通过id查询")
	@ApiOperation(value="fac_classroom-通过id查询", notes="fac_classroom-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		FacClassroom facClassroom = facClassroomService.getById(id);
		if(facClassroom==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(facClassroom);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param facClassroom
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FacClassroom facClassroom) {
        return super.exportXls(request, facClassroom, FacClassroom.class, "fac_classroom");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FacClassroom.class);
    }

}
